<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>动态组件的切换和状态保存</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/3.0.2/vue.global.js"></script>
  </head>

  <body>
    <div id="app"></div>
  </body>
  <script>
    const app = Vue.createApp({
      data() {
        return {
          showItem: "liuying",
        };
      },
      template: ` 
            <h2>欢迎光临-请选择您</h2> 
            <button style="display:block" @click="handleClick">切换</button>
            <hr/>
            <keep-alive>
            <component :is="showItem" />
            </keep-alive>
        `,
      methods: {
        handleClick() {
          this.showItem = this.showItem == "dajiao" ? "liuying" : "dajiao";
        },
      },
    });

    app.component("dajiao", {
      template: `<input />`,
    });

    app.component("liuying", {
      template: `<img src="https://newimg.jspang.com/liuying01.jpg" />`,
    });
    const vm = app.mount("#app");
  </script>
</html>
